package forum;
import javax.persistence.*;
import javax.ejb.*;
import agent.*;
import agent.AgentEJB;
import java.util.Date;
import java.util.ArrayList;

/**
 * EJB de gestion des Posts
 * @author Geoffroy
 *
 */
@Stateless
@Local
public class PostEJB implements PostInterface {

	@PersistenceContext
	EntityManager em;
	@EJB
	TopicInterface topicManager;
	@EJB
	AgentInterface agentManager;
	
	
	
	public Post findById(int idPost) {
		return (Post)em.find(Post.class,idPost);
	}
	
	public void addPost(Post p) {
		em.persist(p);
	}
	
	public void update(Post p) {
		em.merge(p);
	}
	
	public ArrayList<Post> findByTopic(int idTopic) {
		String HQLQuery = "SELECT p FROM Post p WHERE p.Topic.id = "+idTopic;
		Query q = em.createQuery(HQLQuery);
		if(q==null) {
			return new ArrayList<Post>();
		}
		else {
			return (ArrayList<Post>)q.getResultList();
		}
	}
}
